<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="comments"
      element-loading-text="加载数据中"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">{{
          scope.$index + (curPage - 1) * limit + 1
        }}</template>
      </el-table-column>
      <el-table-column label="头像" align="center" width="80">
        <template slot-scope="scope">
           <el-avatar shape="square" size="small" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" width="150">
        <template slot-scope="scope">{{ scope.row.nickname }}</template>
      </el-table-column>
       <el-table-column label="评论文章" align="center" width="150">
        <template slot-scope="scope">{{ scope.row.blog.title }}</template>
      </el-table-column>
      <el-table-column label="评论内容" align="center">
        <template slot-scope="scope">{{ scope.row.content }}</template>
      </el-table-column>
      <el-table-column label="评论日期" align="center" width="230">
        <template slot-scope="scope">{{ scope.row.createDate }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="1500"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteComment(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        style="margin-top: 30px"
        background
        :page-size="limit"
        :page-sizes="[1, 2, 5, 10, 20]"
        layout="prev, pager, next, total, ->, sizes, jumper"
        :total="count"
        :current-page.sync="curPage"
        @prev-click="prevClickHandle"
        @next-click="nextClickHandle"
        @current-change="currentChangeHandle"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getComment, delComment } from "@/api/comment.js";
import { formatDate } from "@/utils/formatDate.js";
export default {
  data() {
    return {
      listLoading: false,
      curPage: 1,
      limit: 5,
      totalPage: 0,
      count: 0,
      comments: [],
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getComment(this.curPage, this.limit).then(({ data }) => {
        this.listLoading = false;
        this.comments = data.rows;
        for(let i of this.comments){
          i.createDate = formatDate(i.createDate);
        }
        this.count = data.total; // 计算总条数
        this.totalPage = Math.ceil(this.count / this.limit); // 总页数
        if (this.curPage > this.totalPage && this.totalPage !== 0) {
          this.curPage = this.totalPage;
          this.fetchData();
        }
      });
    },
    deleteComment({ id }) {
      this.$confirm("是否删除此条评论信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          delComment(id).then((res) => {
            this.fetchData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    prevClickHandle() {
      this.curPage -= 1;
    },
    nextClickHandle() {
      this.curPage += 1;
    },
    currentChangeHandle(pageNum) {
      this.curPage = pageNum;
      this.fetchData();
    },
    handleSizeChange(pagerNum) {
      this.curPage = 1;
      this.limit = pagerNum;
      this.fetchData();
    },
  },
};
</script>

